﻿using System;
using System.IO;
using WatiNCF;
using WatiNCF.Extensions;
using WatiN.Core;
using WatiN.Core.Interfaces;

namespace WatiNCF.UnitTests
{
    public abstract class BaseWatinTest<T> : IWatinTest
        where T : class, IHomePageController
    {
        private int _timeOut = 15;
        private T _homePage;

        static BaseWatinTest()
        {
            Settings.Instance.MakeNewIeInstanceVisible = true;
        }

        public T HomePage
        {
            get
            {
                if (_homePage == null)
                {
                    _homePage = Activator.CreateInstance<T>();
                }
                return _homePage;
            }
        }

        public string HomePageUrl
        {
            get { return HomePage.HomePageUrl;  }   
        }

        public IE Browser
        {
            get { return ((IHomePageController)HomePage).Browser; }
            set { ((IHomePageController)HomePage).Browser = value; }
        }

        public bool CloseBrowser { get; set; }

        public virtual void ClassInitialize()
        {
        }

        public virtual void ClassCleanUp()
        {
        }


        public virtual void TestInitialize()
        {
            CloseBrowser = true;
            ((IHomePageController)HomePage).NavigateToHomePage();
            Browser.Refresh(); // Avoid cached data and start from clean slate
        }

        public virtual void TearDown()
        {
            if (CloseBrowser)
            {
                ((IHomePageController) HomePage).Browser.Close();
                ((IDisposable) HomePage).Dispose();
            }
        }

        public virtual void SetShortTimeOut()
        {
            StoreTimeOut();
            Settings.Instance.WaitForCompleteTimeOut = 5;
            Settings.Instance.WaitUntilExistsTimeOut = 5;
            Settings.Instance.AttachToIETimeOut = 5;
        }

        public virtual void SetMediumTimeOut()
        {
            StoreTimeOut();
            Settings.Instance.WaitForCompleteTimeOut = 15;
            Settings.Instance.WaitUntilExistsTimeOut = 15;
            Settings.Instance.AttachToIETimeOut = 15;
        }

        public virtual void SetDefaultTimeOut()
        {
            StoreTimeOut();
            Settings.Instance.WaitForCompleteTimeOut = 30;
            Settings.Instance.WaitUntilExistsTimeOut = 30;
            Settings.Instance.AttachToIETimeOut = 30;
        }

        public virtual void SetLongTimeOut()
        {
            StoreTimeOut();
            Settings.Instance.WaitForCompleteTimeOut = 60;
            Settings.Instance.WaitUntilExistsTimeOut = 60;
            Settings.Instance.AttachToIETimeOut = 60;
        }

        private void StoreTimeOut()
        {
            _timeOut = Settings.Instance.WaitForCompleteTimeOut;
        }

        public virtual void ResetTimeOut()
        {
            Settings.Instance.WaitForCompleteTimeOut = _timeOut;
            Settings.Instance.WaitUntilExistsTimeOut = _timeOut;
            Settings.Instance.AttachToIETimeOut = _timeOut;
        }
    }
}